package com.cn.springcloudwechat.task.job.weixn;
import com.cn.springcloudwechat.common.utils.SpringWebContextUtil;
import com.cn.springcloudwechat.model.task.base.ScheduleJob;
import com.cn.springcloudwechat.service.find.askDoctor.AskDoctorService;
import com.cn.springcloudwechat.task.utils.ScheduleUtils;
import com.cn.springcloudwechat.task.utils.TaskLogUtil;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;

public class AskDoctorJob implements Job {
    @Autowired
    AskDoctorService askDoctorService;
    /* 日志对象 */
    private static final Logger LOG = LoggerFactory.getLogger(AskDoctorJob.class);

    @Override
    public void execute(JobExecutionContext context){
        ScheduleJob scheduleJob= (ScheduleJob)context.getMergedJobDataMap().get(ScheduleUtils.JOB_PARAM_KEY);
        String jobName=scheduleJob.getJobName();
        String jobGroup=scheduleJob.getJobGroup();
        String jobClass=scheduleJob.getJobClass();
        LOG.info("任务[" + jobName + "]成功运行");
        try {
            ApplicationContext ac = SpringWebContextUtil.getApplicationContext();
            askDoctorService.updateTotalQuota();
            // 定时任务-重置医生提问数
            TaskLogUtil.saveTaskLog(jobGroup + ":" + jobName, jobClass,TaskLogUtil.NORMAL, "微信同步关注者任务正常运行");
        } catch (Exception e) {
            LOG.error("任务[" + jobName + "]异常",e);
            // 保存异常日志
            TaskLogUtil.saveTaskLog(jobGroup + ":" + jobName, jobClass,TaskLogUtil.EXCEPTION,e.toString());
        }
    }

}
